define(function(require, exports) {
	require("js/jquery-2.1.4.js");
	var framework = require("manage/js/framework.js");
	require('manage/js/choiceTime.js');
	//require("${appPath}/js/ajaxfileupload.js");
	framework.setCurrentMenu("PAGE_001");
	checkIsLogin();
	
	search();
	
	function checkIsLogin(){
		var admin = sessionStorage.getItem("admin");
	    $("#userNU .userC").find("dt").html(admin);
	    $("#userNU .userC").find("dt").attr("title",admin);
		if(admin==null || admin==""){
			window.location.href="http://localhost:8080/erhuogo/manage/login.html";
		}
	}
	// 查找函数
	function search() {
		var page = getHashPage();
		if (page == null) {
			page = 1;
		}
		searchHandler(page);
	}

	function searchHandler(page) {
		page = parseInt(page);
		window.location.hash = "#" + page;
		clearTable();
		initTable(page);
		bindOnChange();
	}

	function clearTable() {
		$(".content .commonTb tr:gt(0)").detach();
		$(".content .paginator span").detach();
	}

	// 初始化列表
	function initTable(page) {
		framework.showLoading();
		/*
		 * var rows = Math.floor(($(window).height() - $('.topNav').height() -
		 * $('.content .noBorderB').height() - $('.content .searchD').height() -
		 * $('.content .commonTb tr:eq(0)').height() - 100 ) / 50); rows = rows <=
		 * 0 ? 1 : rows;
		 */
		var rows = 20;
		var nickName = $("#searchName").val();
		var id = $("#searchId").val();
		var date = $("#datePicker").val();
		var startDate = $('input[id="startDate"]').val();
		var endDate = $('input[id="endDate"]').val();
		
		$.ajax({
			url:"member_listPage.action",
			type:"get",
			dataType:"json",
			data:{
				"page":page,
				"rows":rows,
				"id":id,
				"nickName":nickName,
				"startDate" : startDate,
				"endDate" : endDate
			},
			success:function(data){
				if (data.data != null && data.data.rows != null) {
					for (var index = 0; index < data.data.rows.length; index++) {
						initTableRow(data.data.rows[index], index + 1);
					}
					if (data.data.records <= rows) {
						$('.paginator').css("display", "none");
					} else {
						$('.paginator').css("display", "");
					}
					initFooder(page, rows, data.data.records);
				}
				framework.hideLoading();
			}
		});
	}

	// 初始化表行
	function initTableRow(rowObject, index) {
		var clazz = "";
		if (index % 2 == 0) {
			clazz = "alt";
		}

		var id = rowObject.id;
		var phoneNumber = rowObject.phoneNumber;
		var nickName = rowObject.nickName;
		var portrait = rowObject.portrait;
		var sex = rowObject.sex;
		var province = rowObject.province;
		var city = rowObject.city;
		var school = rowObject.school;
		var shieldStatus = rowObject.shieldStatus;
		var regTime = longTrans2Date(rowObject.regTime);
		var alterTime = longTrans2Date(rowObject.alterTime);
		
		var memStatus = "";
		if (shieldStatus=="0"){
			memStatus = "启用";
		}else if(shieldStatus=="1"){
			memStatus = "屏蔽";
		}
		var _sex = "";
		if (sex=="0"){
			_sex = "女";
		}else if(sex=="1"){
			_sex = "男";
		}
		
		var row = $("<tr/>");
		var tdDiv = "";
		if(shieldStatus=="0"){
			tdDiv += '<div class="editM1" title="屏蔽"><a href="javascript:void(0);">屏蔽</a></div>';
		}else if(shieldStatus=="1"){
			tdDiv += '<div class="editM1" title="启用"><a href="javascript:void(0);">启用</a></div>';
		}
		//tdDiv += '<div class="editM1" title="屏蔽"><a href="javascript:void(0);">屏蔽</a></div>';
		//tdDiv += '<div class="editM2" title="删除"><a href="javascript:void(0);">删除</a></div>';
		row.append('<td class="operateTd_d" style="line-height: 28px;" id="_operateTd_'
						+ index + '">' + tdDiv + '</td>');

		var img = "<img class='viewImg' src='"+portrait+"'  height='20px'/>"
		row.append('<td title="'+ id +'" style="text-align:center">'+ id +'</>');
		row.append('<td title="'+ phoneNumber +'" style="text-align:center">'+ phoneNumber +'</>');
		row.append('<td title="'+ nickName +'" style="text-align:center">'+ nickName +'</>');
		row.append('<td title="'+ portrait +'" style="text-align:center">'+ img +'</>');
		
		row.append('<td title="'+ _sex +'" style="text-align:center">'+ _sex +'</>');
		row.append('<td title="'+ province +'" style="text-align:center">'+ province +'</>');
		row.append('<td title="'+ city +'" style="text-align:center">'+ city +'</>');
		row.append('<td title="'+ school +'" style="text-align:center">'+ school +'</>');
		row.append('<td title="'+ memStatus +'" style="text-align:center">'+ memStatus +'</>');
		row.append('<td title="'+ regTime +'" style="text-align:center">'+ regTime +'</>');
		row.append('<td title="'+ alterTime +'" style="text-align:center">'+ alterTime +'</>');
		row.addClass(clazz);
		row.hover(function() {
			row.removeClass(clazz);
			row.addClass("over");
		}, function() {
			row.removeClass("over");
			row.addClass(clazz);
		});
		row.attr("memId", id);
		$(".content .commonTb").append(row);

		// 屏蔽点击事件
		$("#_operateTd_" + index + " .editM1").click(function() {
			var transStatus = "";
			var _operate = "";
			if(shieldStatus=="0"){
				transStatus = 1;
				_operate = "屏蔽";
			}else if(shieldStatus=="1"){
				transStatus = 0;
				_operate = "启用";
			}
			framework.confirmss("确定"+_operate+"用户:"+nickName+"?",
				function(){
					$.ajax({
						url:"member_updateStatusById.action",
						type:"get",
						dataType:"json",
						data:{
							"id":id,
							"status":transStatus
						},
						success:function(data){
							if(data.result=="SUCCESS"){
								search();
								framework.hint(_operate+"成功");
							}else{
								search();
								framework.hint(_operate+"失败");
							}
						}
					})
				},function(){
					
				})
		});

	    function IsNum(num){
	    	var reNum=/^\d*$/;
	    	return(reNum.test(num));
		}

		// 删除点击事件
		$("#_operateTd_" + index + " .editM2").click(function() {
			framework.confirmss("确定删除演员:"+name+"?",
			function(){
				$.ajax({
					url:"/actors/delete",
					type:"get",
					data:{"id":id},
					success:function(data){
						if(data!=null){
							framework.hint(data.message);
							search();
						}
					}
				})
			},function(){
				
			})
		});

		row.find(".appName").click(function() {
			window.location.href = "./questionList.html?phaseId=" + id;
		});
		
	}
	


	// 初始化脚
	// 参数： 第几页 行数 数量
	function initFooder(page, rows, count) {
		$('.content .paginator').append(
				'<span class="prev">上一页</span> '
						+ ' <span class="next">下一页</span>'
						+ '<span class="right">共</span> '
						+ '<div class="clear"></div>');
		var pageCount = Math.ceil(count / rows);
		var spanArray = new Array();
		spanArray.push(page);
		var i = 1;
		var mix = 5 > pageCount ? pageCount : 5;
		while (spanArray.length < mix) {
			if (page + i <= pageCount) {
				spanArray.push(page + i);
			}
			if (page - i > 0) {
				spanArray.unshift(page - i);
			}
			i++;
		}
		var firstCurrent = '';
		var lastCurrent = '';
		if (page == pageCount) {
			lastCurrent = 'class="current"';
		}
		if (page == 1) {
			firstCurrent = 'class="current"';
		}
		if (page <= pageCount - 3) {
			$('.content .paginator .prev:last').after(
					$('<span ' + lastCurrent + '></span>').append(
							initFooderPage(pageCount, pageCount))).after(
					$('<span></span>').append('...'));
		} else {
			$('.content .paginator .prev:last').after(
					$('<span ' + lastCurrent + '></span>').append(
							initFooderPage(pageCount, pageCount)));
		}
		for (var index = spanArray.length - 2; index >= 1; index--) {
			if (spanArray[index] == page) {
				$('.content .paginator .prev:last').after(
						$('<span class="current">' + page + '</span>'));
			} else {
				$('.content .paginator .prev:last').after(
						$('<span></span>').append(
								initFooderPage(spanArray[index],
										spanArray[index])));
			}
		}
		if (page > 3) {
			$('.content .paginator .prev:last').after(
					$('<span ' + firstCurrent + '></span>').append('...'))
					.after($('<span></span>').append(initFooderPage(1, 1)));
		} else {
			$('.content .paginator .prev:last').after(
					$('<span ' + firstCurrent + '></span>').append(
							initFooderPage(1, 1)));
		}

		if (page != 1) {
			$('.content .paginator .prev:first').html(
					initFooderPage(page - 1, '上一页'));
		}

		if (page != pageCount) {
			$('.content .paginator .next:first').html(
					initFooderPage(page + 1, '下一页'));
		}
		$('.content .paginator a').click(function() {
			searchHandler(parseInt($(this).attr('page')));
		});

		$('.content .paginator .right:last')
				.before(
						$('<span class="leftbd20"><img id="pgSubmit" src="./manage/images/footer_submit.png"></span>'))
				.before($('<span class="leftbd7"></span>').append('页'))
				.before(
						$('<span class="leftbd7"><input type="text" id="pgnum" class="text40" value="'
								+ page + '"></span>')).before(
						$('<span class="leftbd20"></span>').append('转到第'))
				.before($('<span class="leftbd2"></span>').append('条')).before(
						$('<span class="pageNum"></span>').append(count));

		$('#pgSubmit').click(function() {
			var pgnum = parseInt($('#pgnum').val());
			if (pgnum >= 1 && pgnum <= pageCount) {
				searchHandler(pgnum);
			} else {
				framework.hint("超出页码范围，一共" + pageCount + "页");
			}
		});
	}

	// 初始化点击脚的内容
	function initFooderPage(page, name) {
		var st = '<a href="javascript:void(0);" page=' + page + '>' + name
				+ '</a>';
		return st;
	}

	function longTrans2Date(time) {
		if (time == null || time == "")
			return "";
		var newTime = new Date(time);
		return newTime.Format("yyyy-MM-dd hh:mm:ss");
	}

	exports.searchList = function() {
		search();
	}

	$(window).bind('hashchange',function(e) {
		if ($('.content .paginator .current').html() == null) {
			return;
		}
		var hashPage = getHashPage();
		if (hashPage == null) {
			return;
		}
		if ($('.content .paginator .current').html()&& $('.content .paginator .current').html() != hashPage) {
			searchHandler(hashPage);
		}
	});

	function getHashPage() {
		var page = null;
		if (window.location.hash) {
			page = window.location.hash;
			if (page.indexOf("#") != -1) {
				page = page.substring(1);
			}
		}
		return page;
	}

	$("#createAppBtn").click(function() {
		initEidit();
		$("input[name='pic_show_way']").eq(0).attr("checked",true);
	})

	$("#imgUpload,#imgUpload1,#imgUpload2,#imgUpload3,#imgUpload4,#imgUpload5").click(function() {
		var obj = $(this).next();
		obj.click();
	});
	
	function bindOnChange(){
		$("#uploadfile").on("change",function(){
			fileUpload($(this).attr("id"));
		})

		$("#uploadfile1").on("change",function(){
			fileUpload($(this).attr("id"));
		})
		
		$("#uploadfile2").on("change",function(){
			fileUpload($(this).attr("id"));
		})
		
		$("#uploadfile3").on("change",function(){
			fileUpload($(this).attr("id"));
		})
		
		$("#uploadfile4").on("change",function(){
			fileUpload($(this).attr("id"));
		})
		
		$("#uploadfile5").on("change",function(){
			fileUpload($(this).attr("id"));
		})
	}
	
	
	function fileUpload(uploadfileId) {
		var img = $('#'+uploadfileId).parent().find("img");
    	$.ajaxFileUpload({
			url : '/upload',
			secureuri : false,
			fileElementId : uploadfileId,
			dataType : 'json',
			success : function(response, status) {
				if (response != null){
					if(response!=""){
						//加入删除图片
						$(img[1]).show();
						$(img[0]).attr("src",response);
	            		img.next().val("true");
	                	framework.hint('上传成功');
					}else{
						framework.hint("上传失败");
						img.next().val("false");
					}
					
                }else{
                	img.next().val("false");
                	//$("#fileContaier .error p").show();
                }
			},
			error : function(data, status, e) {
				framework.hint("上传发生异常");
			}
		})
    }

	$(".uploadImg").each(function() {
		$(this).click(function() {
			var id = $(this).parent().next().next().attr("id");
			$(this).hide();
			$(this).parent().find("img").eq(0).attr("src", "");
			//移除file input
			$(this).parent().next().next().remove();
			//添加file input
			var html = '<input type="file" name="file" id="'+id+'" accept="image/jpeg,image/png" style="display:none">';
			$(this).parent().parent().append(html);
			//绑定事件
			$("#"+id).on("change",function(){
				fileUpload($(this).attr("id"));
			})
		})
	})
	
	$("#saveActor").click(function(){
		saveActor();
	})
	
	function edit(actor){
		$("#actor").show();
		$(".maskbg").show();
		$(".uploadImg").show();
		$("input[name='pic_show_way'][value='"+actor.picShowWay+"']").prop("checked",true);
		$("#select").find("option[value='"+actor.group+"']").prop("selected",true);
		$("#actorId").val(actor.id);
		$("#name").val(actor.name);
		$("#summary").val(actor.summary);
		$("#code").val(actor.code);
		$("#img0").attr("src",actor.headimgUrl);
		$("#img1").attr("src",actor.bigImg);
		$("#img2").attr("src",actor.img1);
		$("#img3").attr("src",actor.img2);
		$("#img4").attr("src",actor.img3);
		$("#img5").attr("src",actor.img4);
	}
	
	//搜索
	$("#searchListButton").click(function(){
		clearTable();
		searchHandler(1);
	})
	//清除
	$("#clearBar").click(function(){
		$("#searchName").val("");
		$("#searchId").val("");
		$("#startDate").val("");
		$("#endDate").val("");
		$('#datePicker').data('dateRangePicker').clear();
	})
	
	$("#cancelCreate").click(function(){
		$("#actor").hide();
		$(".maskbg").hide();
		$(".uploadImg").hide();
	})
	
	function initEidit(){
		$("#actor").show();
		$(".maskbg").show();
		$(".uploadImg").hide();
		$("#actorId").val("");
		$("#name").val("");
		$("#summary").val("");
		$("#img0").attr("src","");
		$("#select").find("option").each(function(){
			$(this).prop("selected",false);
		})
		$("#code").val("");
		$("#img1").attr("src","");
		$("#img2").attr("src","");
		$("#img3").attr("src","");
		$("#img4").attr("src","");
		$("#img5").attr("src","");
		// $("input[name='pic_show_way']").each(function(){
		// 	$(this).prop("checked",true);
		// })
	}
	
	function saveActor(){
		var id = $("#actorId").val();
		var name = $.trim($("#name").val());
		var summary = $.trim($("#summary").val());
		var headimgUrl = $("#img0").attr("src");
		var group = $("select option:selected").val();
		var code = $.trim($("#code").val());
		var bigImg = $("#img1").attr("src");
		var img1 = $("#img2").attr("src");
		var img2 = $("#img3").attr("src");
		var img3 = $("#img4").attr("src");
		var img4 = $("#img5").attr("src");
		var picShowWay = $("input[name='pic_show_way']:checked").val();
		if(name==""){
			framework.hint("请输入姓名");
			return ;
		}
		if(summary==""){
			framework.hint("请输入简介");
			return ;
		}
		if(headimgUrl==null||headimgUrl==""){
			framework.hint("请上传头像");
			return ;
		}
		if(group==0){
			framework.hint("请选择分组");
			return ;
		}
		if(code==""){
			framework.hint("请输入编码");
			return ;
		}
		if(bigImg==null||bigImg==""){
			framework.hint("请上传图片1");
			return ;
		}
		if(img1==null||img1==""){
			framework.hint("请上传图片2");
			return ;
		}
		if(img2==null||img2==""){
			framework.hint("请上传图片3");
			return ;
		}
		if(img3==null||img3==""){
			framework.hint("请上传图片4");
			return ;
		}
		if(img4==null||img4==""){
			framework.hint("请上传图片5");
			return ;
		}
		if(picShowWay==null){
			framework.hint("请选择图片结构");
			return ;
		}
		var param = {"id":id,"name":name,"summary":summary,"headimgUrl":headimgUrl,
				"group":group,"code":code,"picShowWay":picShowWay,"bigImg":bigImg,
				"img1":img1,"img2":img2,"img3":img3,"img4":img4 }
		$.ajax({
			url:"/actors/save",
			type:"post",
			data:param,
			success:function(data){
				if(data.result=="SUCCESS"){
					$("#actor").hide();
					$(".maskbg").hide();
					if(id!=""){
						framework.hint("修改成功");
					}else{
						framework.hint("添加成功");
					}
					search();
				}else{
					$("#actor").hide();
					$(".maskbg").hide();
					framework.hint("添加失败");
					search();
				}
			}
		})
	}
});
